package com.door.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.Material;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.ColorService;
import com.door.erp.sevice.MaterialService;

/**
 * 原材料Controller
 * @author yangxuelin
 */
@Controller
public class MaterialController {
	
	@Resource(name="materialService", type=MaterialService.class)
	private MaterialService materialService;
	
	@Resource(name="colorService", type=ColorService.class)
	private ColorService colorService;
	
	@RequestMapping("/material/add.do")
	public ModelAndView add(HttpServletRequest request, Material material) {
		material.setStatus(BaseInfoStatus.NORMAL);
		materialService.add(material);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("material");
		resonse.setMessage("添加成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/material/addPage.do")
	public String addPage(HttpServletRequest request) {
		return "material/add";
	}

	@RequestMapping("/material/materials.do")
	public String materials(HttpServletRequest request, Pager pager) {
		int totalCount = materialService.getCount();
		if(pager == null) {
			pager = new Pager();
		}
		pager.setTotalCount(totalCount);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		List<Material> materials = materialService.getAll(params, pager);
		request.setAttribute("materials", materials);
		request.setAttribute("pager", pager);
		return "material/materials";
	}
	
	@RequestMapping("/material/delete.do")
	public ModelAndView delete(HttpServletRequest request, long id) {
		Material material = materialService.getObject(id);
		material.setStatus(BaseInfoStatus.DELETED);
		materialService.modify(material);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("material");
		resonse.setCallbackType("");
		resonse.setMessage("删除成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/material/getMaterial.do")
	public String getMaterial(HttpServletRequest request, long id) {
		Material material = materialService.getObject(id);
		request.setAttribute("material", material);
		return "material/modify";
	}
	
	@RequestMapping(value = "/material/get/{id}.do")
	public ModelAndView find(@PathVariable(value="id") Long id) {
		ModelAndView mv = new ModelAndView("material/detail");
		Material material = materialService.getObject(id);
		mv.addObject("material", material);
		return mv;
	}
	
	@RequestMapping("/material/modify.do")
	public ModelAndView modify(Material material) {
		materialService.modify(material);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("material");
		resonse.setMessage("修改成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/material/lookuplist.do")
	public ModelAndView lookuplist(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		ModelAndView mv = new ModelAndView("material/material_lookup");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = materialService.getCount(params);
		pager.setTotalCount(totalCount);
		List<Material> materials = materialService.getAll(params, pager);
		mv.addObject("materials", materials);
		return mv;
	}
}
